package com.jinzw.jform.section

import android.content.Context
import android.graphics.Color
import android.support.v7.widget.AppCompatTextView
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import com.jinzw.jform.core.DimensionUtils
import com.jinzw.jform.rows.Row

/**
 * Created by jin on 18-2-11.
 */
class MultivaluedSection constructor(private val context:Context,var title:String):ABstractSection {

    override fun onCreateView(): View {
        val layout = LinearLayout(context)
        layout.orientation = LinearLayout.VERTICAL

        titleView = createSectionTitleView() //绘制section标题
        layout.addView(titleView, titleView?.layoutParams)

        for (index in 0 until rows.size) {
            /*    if (islines) {
                    var linesparams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 2)
                    linesparams.setMargins(dpToPx(20),0, dpToPx(20),0)
                    var viewline = View(context)

                    viewline.setBackgroundColor(Color.BLACK)
                    viewline.layoutParams = linesparams
    //                layout.addView(viewline)
                }*/
            val row = rows[index]
            row.create()
            layout.addView(row.view)
            if (index != rows.size - 1) {
                layout.addView(onCreateDivider(index))
            }
        }

        layout.layoutParams = ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        return layout
    }

    companion object {
        val TITLE_DEFAULT_MARGIN_TOP = 6
        val TITLE_DEFAULT_MARGIN_BOTTOM = 6
        val TITLE_DEFAULT_MARGIN_LEFT = 16
        val TITLE_DEFAULT_MARGIN_RIGHT = 16
    }

    var id = -1
    var islines = false
    var titleTextColor = Color.parseColor("#2196F3")
    var rowDividerColor = Color.TRANSPARENT
    var rowDividerHeight = DimensionUtils.dpToPx(2)

    var titleView: View? = null

    override val rows = mutableListOf<Row<*>>()

    fun <R:Row<*>> row(row:R,block:(R.()->Unit)):MultivaluedSection{
        rows.add(row)
        block.invoke(row)
        return this
    }
    var onCreateDivider: ((Int) -> View) = {
        val divider = View(context)

        rowDividerColor = Color.parseColor("#F6F6F6")
        divider.setBackgroundColor(rowDividerColor)
        divider.layoutParams = ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, rowDividerHeight)
        divider
    }
    /**
     * Implementation of the TitleView visual aspect
     */
    var customizeTitleView: ((MultivaluedSection, TextView) -> Unit) = { row, textView ->

    }



    /**
     * Use this lambda to change the visual aspect of the TitleView
     */
    protected fun createSectionTitleView(): TextView {

        val titleView = AppCompatTextView(context)
        titleView.text = title
        val layoutParams = ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        titleView.layoutParams = layoutParams

        //margin not working here
        titleView.setPadding(
                DimensionUtils.dpToPx(TITLE_DEFAULT_MARGIN_LEFT),
                DimensionUtils.dpToPx(TITLE_DEFAULT_MARGIN_TOP),
                DimensionUtils.dpToPx(TITLE_DEFAULT_MARGIN_BOTTOM),
                DimensionUtils.dpToPx(TITLE_DEFAULT_MARGIN_RIGHT))

        titleView.layoutParams = layoutParams
        titleView.setTextColor(titleTextColor)
        titleView.text = this.title
        titleView.textSize = 16f

        customizeTitleView.invoke(this, titleView)

        return titleView
    }
}